//
//  UIViewController+Function.m
//  SevenDressHD
//
//  Created by czl on 15/11/26.
//  Copyright © 2015年 chi. All rights reserved.
//

#import "UIViewController+Function.h"

@interface myView : UIView
@end
@implementation myView
- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    float offset = 1.5;
    float my = rect.size.height/2.0;
    float mx = rect.size.width/2.0;
    UIBezierPath* aPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(mx-2*offset, my-offset, 4*offset, 2*offset)];
    [aPath moveToPoint:CGPointMake(0, my)];
    [aPath addLineToPoint:CGPointMake(mx, my-offset)];
    [aPath addLineToPoint:CGPointMake(2*mx, my)];
    [aPath addLineToPoint:CGPointMake(mx, my+offset)];
    [aPath closePath];
    // Set the render colors
    [[UIColor blackColor] setStroke];
    [[UIColor whiteColor] setFill];
    [aPath fill];
}
@end

@implementation UIViewController (Function)

-(void)closeAnimate:(dispatch_block_t)doneBlock {
    CGRect screen = [UIScreen mainScreen].bounds;
    UIView* blackView = [[UIView alloc]initWithFrame:screen];
    blackView.backgroundColor = [UIColor blackColor];
    UIView* whiteView = [[UIView alloc]initWithFrame:screen];
    whiteView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:blackView];
    [blackView addSubview:whiteView];
    [self beginExitAnimation:whiteView blackView:blackView block:doneBlock];
}

-(void)animateOut:(UIView *)theView bView:(UIView*)bview block:(dispatch_block_t)doneBlock{
    [UIView animateWithDuration:0.5 animations:^{
        theView.transform = CGAffineTransformMakeScale(1, 0.005);
    } completion:^(BOOL finished){
        myView *view = [[myView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        view.center = bview.center;
        [bview addSubview:view];
        [UIView animateWithDuration:0.4 animations:^{
            theView.transform = CGAffineTransformMakeScale(0, 0);
            view.transform = CGAffineTransformMakeScale(1, 0.0000001);
            //退出
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [theView removeFromSuperview];
                if (doneBlock) {
                    doneBlock();
                }
            });
        }];
    }];
}

-(void)beginExitAnimation:(UIView *)view blackView:(UIView*)bView block:(dispatch_block_t)doneBlock{
    [self animateOut:view bView:bView block:doneBlock];
}


@end
